﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Scorpicore.MVC.Helpers
{
    public static class Calculations
    {
        public static double RadianToDegree(double radians)
        {
            return radians * (180 / Math.PI);
        }

        public static double DegreeToRadians(double degrees)
        {
            return degrees * (Math.PI / 180);
        }  

        public static double DistanceBetweenPoints(Point first, Point second)
        {
            const int RaiusOfEarth = 6371;
            double distance = 0;
            Point firstInRadians = new Point();
            Point secondInRadians = new Point();

            firstInRadians.Latitude = DegreeToRadians(first.Latitude);
            firstInRadians.Longitude = DegreeToRadians(first.Longitude);
            secondInRadians.Latitude = DegreeToRadians(second.Latitude);
            secondInRadians.Longitude = DegreeToRadians(second.Longitude);
            double i = (Math.Cos(firstInRadians.Latitude) * Math.Cos(firstInRadians.Longitude)*
                Math.Cos(secondInRadians.Latitude) * Math.Cos(secondInRadians.Longitude) +
                Math.Cos(firstInRadians.Latitude) * Math.Sin(firstInRadians.Longitude) *
                Math.Cos(secondInRadians.Latitude) * Math.Sin(secondInRadians.Longitude) +
                Math.Sin(firstInRadians.Latitude) * Math.Sin(secondInRadians.Latitude));
            double j = (Math.Acos(i));
            distance = (RaiusOfEarth * j);

            return distance;
        }
    }
}